cmake_minimum_required(VERSION 3.16)
project(AI_CTP VERSION 1.0.0 LANGUAGES CXX)

# 设置C++标准
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置编译器特定选项
if(MSVC)
    # Windows Visual Studio 编译选项
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj /utf-8 /wd4828 /wd4996 /EHsc")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /utf-8 /wd4828 /wd4996")
    
    # 禁用特定警告
    add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
    add_compile_definitions(_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING)
else()
    # GCC/Clang 编译选项
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Wextra")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -finput-charset=utf-8 -fexec-charset=utf-8")
endif()

# 寻找Python
find_package(Python COMPONENTS Interpreter Development REQUIRED)
message(STATUS "Found Python: ${Python_EXECUTABLE}")
message(STATUS "Python version: ${Python_VERSION}")

# 寻找pybind11 - 简化版本
find_package(pybind11 CONFIG QUIET)
if(NOT pybind11_FOUND)
    # 尝试通过pip安装的pybind11
    execute_process(
        COMMAND ${Python_EXECUTABLE} -c "import pybind11; print(pybind11.get_cmake_dir())"
        OUTPUT_VARIABLE pybind11_DIR
        OUTPUT_STRIP_TRAILING_WHITESPACE
        RESULT_VARIABLE pybind11_RESULT
        ERROR_QUIET
    )
    
    if(pybind11_RESULT EQUAL 0 AND EXISTS ${pybind11_DIR})
        message(STATUS "Found pybind11 via Python: ${pybind11_DIR}")
        find_package(pybind11 CONFIG REQUIRED PATHS ${pybind11_DIR})
    else()
        message(FATAL_ERROR "pybind11 not found. Please install with: pip install pybind11")
    endif()
endif()

message(STATUS "pybind11 found: ${pybind11_VERSION}")

# CTP API路径设置
if(WIN32)
    set(CTP_API_ROOT "E:/2_ctp_office/v6.7.8_traderapi_20240918/v6.7.8_traderapi_20240918/v6.7.8_20240918_winApi/traderapi/20240918_traderapi64_se_windows")
    set(CTP_LIBRARIES 
        "${CTP_API_ROOT}/thostmduserapi_se.lib"
        "${CTP_API_ROOT}/thosttraderapi_se.lib"
    )
    set(CTP_DLLS
        "${CTP_API_ROOT}/thostmduserapi_se.dll"
        "${CTP_API_ROOT}/thosttraderapi_se.dll"
    )
else()
    # Linux路径
    set(CTP_API_ROOT "/path/to/ctp/linux/api")
    set(CTP_LIBRARIES 
        "${CTP_API_ROOT}/thostmduserapi_se.so"
        "${CTP_API_ROOT}/thosttraderapi_se.so"
    )
endif()

# 检查CTP API文件是否存在
foreach(LIB ${CTP_LIBRARIES})
    if(NOT EXISTS ${LIB})
        message(WARNING "CTP library not found: ${LIB}")
    else()
        message(STATUS "Found CTP library: ${LIB}")
    endif()
endforeach()

# 设置头文件路径
include_directories(
    ${CTP_API_ROOT}
    ${CMAKE_SOURCE_DIR}/include
    ${CMAKE_SOURCE_DIR}/include/performance
    ${CMAKE_SOURCE_DIR}/include/factory
)

# 收集源文件
file(GLOB SOURCES
    "src/md_api.cpp"
    "src/trader_api.cpp"
)

# 添加Python绑定文件
list(APPEND SOURCES "python_binding.cpp")

# 创建Python模块
pybind11_add_module(ai_ctp ${SOURCES})

# 链接CTP库
target_link_libraries(ai_ctp PRIVATE ${CTP_LIBRARIES})

# 设置目标属性
target_compile_definitions(ai_ctp PRIVATE VERSION_INFO=${PROJECT_VERSION})

# Windows特定设置
if(WIN32)
    # 复制CTP DLL到输出目录
    foreach(DLL ${CTP_DLLS})
        if(EXISTS ${DLL})
            add_custom_command(TARGET ai_ctp POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy_if_different
                "${DLL}"
                $<TARGET_FILE_DIR:ai_ctp>
                COMMENT "Copying ${DLL}"
            )
        endif()
    endforeach()
endif()

# 安装设置
install(TARGETS ai_ctp DESTINATION .)
if(WIN32)
    foreach(DLL ${CTP_DLLS})
        if(EXISTS ${DLL})
            install(FILES ${DLL} DESTINATION .)
        endif()
    endforeach()
endif() 